package com.example.study.androidtools.storage;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by shangsong on 17-2-22.
 */

public class StorageStudy {
    private static final String TAG = StorageStudy.class.getSimpleName();
    private final Context context;

    public StorageStudy(Context context) {
        this.context = context;
    }

    public void start() {
        showInternalStoragePath();
        String fileName = "test_internal_storage_file";
        String content = "##test write content to internal storage file##";
        createFileOnInternalStorage(fileName);
        writeStringToFile(fileName, content);
        readFromInternalStorageFile(fileName);
        showInternalStorageFileList();
        deleteInternalStorageFile(fileName);
        getInternalStorageFile("dir1");

        showExternalStoragePublicDirectory();
        showExternalStorageDirectory();
        showExternalFilesDir();
        showDownloadCacheDirectory();
        showDataDirectory();
    }

    //路径：/data/data/package name/files
    public void showInternalStoragePath() {
        Log.i(TAG, "showInternalStoragePath: path=" + context.getFilesDir());
    }

    //路径：/data/data/package name/files/
    public void createFileOnInternalStorage(String fileName) {
        File file = new File(context.getFilesDir(), fileName);
        Log.i(TAG, "createFileOnInternalStorage: file path=" + file.getAbsolutePath());
    }

    //路径：/data/data/package name/files/
    public void writeStringToFile(String fileName, String content) {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            fileOutputStream.write(content.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fileOutputStream) {
                try {
                    fileOutputStream.flush();
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    //路径：/data/data/package name/files/
    public void readFromInternalStorageFile(String fileName) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = context.openFileInput(fileName);
            byte[] buffer = new byte[4096];
            int length;
            while ((length = fileInputStream.read(buffer)) != -1) {
                Log.i(TAG, "read from internal storage: content=" + new String(buffer, 0, length));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fileInputStream) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //路径：/data/data/package name/files/
    public void showInternalStorageFileList() {
        String[] fileList = context.fileList();
        for (String file : fileList) {
            Log.i(TAG, "showInternalStorageFileList: " + file);
        }
    }

    public void deleteInternalStorageFile(String fileName) {
        if (context.deleteFile(fileName)) {
            Log.i(TAG, "delete file " + fileName + " successfully");
        } else {
            Log.i(TAG, "failed to delete file " + fileName);
        }
    }

    //创建一个目录 path：/data/data/package name/
    public void getInternalStorageFile(String dirName) {
        File file = context.getDir(dirName, Context.MODE_PRIVATE);
        Log.i(TAG, "getInternalStorageFile: file path=" + file.getAbsolutePath());
    }


    //路径：/storage/emulated/0/DCIM
    public void showExternalStoragePublicDirectory() {
        File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        Log.i(TAG, "showExternalStoragePublicDirectory: path=" + file);
    }

    //path: /storage/emulated/0
    public void showExternalStorageDirectory() {
        File file = Environment.getExternalStorageDirectory();
        Log.i(TAG, "showExternalStorageDirectory: path=" + file);
    }

    // storage/emulated/0/Android/data/com.example.study.androidtools/files/extra_test
    public void showExternalFilesDir() {
        String filePath = context.getExternalFilesDir("extra_test").getAbsolutePath();
        Log.i(TAG, "getExternalFilesDir: path=" + filePath);
    }

    // /cache
    public void showDownloadCacheDirectory() {
        Log.i(TAG, "showDownloadCacheDirectory: path=" + Environment.getDownloadCacheDirectory());
    }

    // /data
    public void showDataDirectory() {
        Log.i(TAG, "showDataDirectory: path=" + Environment.getDataDirectory());
    }
}
